scrapbox objectに追加されたAPIを調べる
経緯
Thu Jun 24 15:53:41 2021
UserScript Events
/icons/hr.icon
2021-07-15
10:48:08 /shokai/shokai.iconさんの解説がキタ!
2021-07-04
09:16:08 methodの使い方も大体わかった
scrapbox.addListener(type: string, listener: () => void)
event typeに対してevent listener listenerを登録する
優先順位は最低になる
別名:scrapbox.on()
scrapbox.removeListener(type: string, listener: () => void)
event listenerの登録を解除する
別名:scrapbox.off()
scrapbox.removeAllListeners(type?: string)
typeに登録されている全てのevent listenerの登録を解除する
typeに何も渡さないと、全てのeventに登録されている全てのevent listenerを登録解除する
scrapbox.once(type: string, listener: () => void)
1回しか実行されないevent listenerを登録する
scrapbox.prependListener(type: string, listener: () => void)
scrapbox.addListener()と同じだが、event発火後最初に呼び出されるようになる
scrapbox.prependOnceListener()
scrapbox.once()と同じだが、event発火後最初に呼び出されるようになる
scrapbox.rawListeners()
scrapbox.listeners(type: string): (() => void)[]
event typeに登録されているevent listenersのリスト
scrapbox.listenerCount(type: string): number
typeに登録されているevent listenerの数
scrapbox.emit(type: string)
event typeを発火する
scrapbox.eventNames(): string[]
event listenerが登録されているevent nameのリスト
scrapbox.getMexListeners(): number
登録できるevent listenerの最大数
default: 10
scrapbox.setMexListeners(length: number)
登録できるevent listenerの最大数を変える
おそらく内部変数
scrapbox._events
scrapbox._eventsCount
scrapbox._maxListeners
07:23:29 eventは4つ
lines:changed
page:changed
ページ遷移すると発火する
scrapbox.Page.titleの変更が間に合っていないっぽい?
layout:changed
project:changed
別のprojectに移動すると発火する
code:js
}), window.scrapbox.Project || (window.scrapbox.Project = {
get name() {
return j.default.CurrentProject.get().name
},
get pages() {
return (0, w.cloneDeep) (j.default.QuickSearch.pages)
}
}), window.scrapbox.Layout || window.scrapbox.__defineGetter__('Layout', () =>j.default.Layout.get()), j.default.Line.addChangeListener(() =>{
requestAnimationFrame(() =>window.scrapbox.emit('lines:changed'))
}), j.default.Page.addChangeListener(({
store: i,
event: s
}) =>{
var _,
w;
'load' === s && (n !== (null === (_ = i.get()) || void 0 === _ ? void 0 : _.title) && (requestAnimationFrame(() =>window.scrapbox.emit('page:changed')), n = null === (w = i.get()) || void 0 === w ? void 0 : w.title))
}), j.default.CurrentProject.addChangeListener(({
store: n,
event: s
}) =>{
var _,
w;
'load' === s && (i !== (null === (_ = n.get()) || void 0 === _ ? void 0 : _.name) && (requestAnimationFrame(() =>window.scrapbox.emit('project:changed')), i = null === (w = n.get()) || void 0 === w ? void 0 : w.name))
}), j.default.Layout.addChangeListener(({
store: n
}) =>{
s !== n.get() && (requestAnimationFrame(() =>window.scrapbox.emit('layout:changed')), s = n.get())
}))
}
06:51:25 これが新しいAPIの定義?
07:17:43 内部APIの定義だった
scrapbox objectの定義は↑にある
code:js
function EventEmitter() {
EventEmitter.init.call(this)
}
n.exports = EventEmitter,
n.exports.once = function once(n, i) {
return new Promise((function (s, _) {
function errorListener(s) {
n.removeListener(i, resolver),
_(s)
}
function resolver() {
'function' == typeof n.removeListener && n.removeListener('error', errorListener),
s([].slice.call(arguments))
}
eventTargetAgnosticAddListener(n, i, resolver, {
once: !0
}),
'error' !== i && function addErrorHandlerIfEventEmitter(n, i, s) {
'function' == typeof n.on && eventTargetAgnosticAddListener(n, 'error', i, s)
}(n, errorListener, {
once: !0
})
}))
},
EventEmitter.EventEmitter = EventEmitter,
EventEmitter.prototype._events = void 0,
EventEmitter.prototype._eventsCount = 0,
EventEmitter.prototype._maxListeners = void 0;
var A = 10;
function checkListener(n) {
if ('function' != typeof n) throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof n)
}
function _getMaxListeners(n) {
return void 0 === n._maxListeners ? EventEmitter.defaultMaxListeners : n._maxListeners
}
function _addListener(n, i, s, _) {
var w,
E,
P;
if (checkListener(s), void 0 === (E = n._events) ? (E = n._events = Object.create(null), n._eventsCount = 0) : (void 0 !== E.newListener && (n.emit('newListener', i, s.listener ? s.listener : s), E = n._events), P = Ei), void 0 === P) P = Ei = s, ++n._eventsCount;
else if ('function' == typeof P ? P = Ei = _ ? [ s,
P
] : [
P,
s
] : _ ? P.unshift(s) : P.push(s), (w = _getMaxListeners(n)) > 0 && P.length > w && !P.warned) {
P.warned = !0;
var A = new Error('Possible EventEmitter memory leak detected. ' + P.length + ' ' + String(i) + ' listeners added. Use emitter.setMaxListeners() to increase limit');
A.name = 'MaxListenersExceededWarning',
A.emitter = n,
A.type = i,
A.count = P.length,
function ProcessEmitWarning(n) {
console && console.warn && console.warn(n)
}(A)
}
return n
}
function onceWrapper() {
if (!this.fired) return this.target.removeListener(this.type, this.wrapFn),
this.fired = !0,
0 === arguments.length ? this.listener.call(this.target) : this.listener.apply(this.target, arguments)
}
function _onceWrap(n, i, s) {
var _ = {
fired: !1,
wrapFn: void 0,
target: n,
type: i,
listener: s
},
w = onceWrapper.bind(_);
return w.listener = s,
_.wrapFn = w,
w
}
function _listeners(n, i, s) {
var _ = n._events;
if (void 0 === _) return [];
return void 0 === w ? [
] : 'function' == typeof w ? s ? [
w.listener || w
] : [
w
] : s ? function unwrapListeners(n) {
for (var i = new Array(n.length), s = 0; s < i.length; ++s) is = ns.listener || ns; return i
}(w) : arrayClone(w, w.length)
}
function listenerCount(n) {
var i = this._events;
if (void 0 !== i) {
if ('function' == typeof s) return 1;
if (void 0 !== s) return s.length
}
return 0
}
function arrayClone(n, i) {
for (var s = new Array(i), _ = 0; _ < i; ++_) s_ = n_; return s
}
function eventTargetAgnosticAddListener(n, i, s, _) {
if ('function' == typeof n.on) _.once ? n.once(i, s) : n.on(i, s);
else {
if ('function' != typeof n.addEventListener) throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof n);
n.addEventListener(i, (function wrapListener(w) {
_.once && n.removeEventListener(i, wrapListener),
s(w)
}))
}
}
Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
enumerable: !0,
get: function () {
return A
},
set: function (n) {
if ('number' != typeof n || n < 0 || P(n)) throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + n + '.');
A = n
}
}),
EventEmitter.init = function () {
void 0 !== this._events && this._events !== Object.getPrototypeOf(this)._events || (this._events = Object.create(null), this._eventsCount = 0),
this._maxListeners = this._maxListeners || void 0
},
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if ('number' != typeof n || n < 0 || P(n)) throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + '.');
return this._maxListeners = n,
this
},
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
return _getMaxListeners(this)
},
EventEmitter.prototype.emit = function emit(n) {
for (var i = [
], s = 1; s < arguments.length; s++) i.push(argumentss); var _ = 'error' === n,
w = this._events;
if (void 0 !== w) _ = _ && void 0 === w.error;
else if (!_) return !1;
if (_) {
var P;
if (i.length > 0 && (P = i0), P instanceof Error) throw P; var A = new Error('Unhandled error.' + (P ? ' (' + P.message + ')' : ''));
throw A.context = P,
A
}
if (void 0 === j) return !1;
if ('function' == typeof j) E(j, this, i);
else {
var B = j.length,
q = arrayClone(j, B);
for (s = 0; s < B; ++s) E(qs, this, i) }
return !0
},
EventEmitter.prototype.addListener = function addListener(n, i) {
return _addListener(this, n, i, !1)
},
EventEmitter.prototype.on = EventEmitter.prototype.addListener,
EventEmitter.prototype.prependListener = function prependListener(n, i) {
return _addListener(this, n, i, !0)
},
EventEmitter.prototype.once = function once(n, i) {
return checkListener(i),
this.on(n, _onceWrap(this, n, i)),
this
},
EventEmitter.prototype.prependOnceListener = function prependOnceListener(n, i) {
return checkListener(i),
this.prependListener(n, _onceWrap(this, n, i)),
this
},
EventEmitter.prototype.removeListener = function removeListener(n, i) {
var s,
_,
w,
E,
P;
if (checkListener(i), void 0 === (_ = this._events)) return this;
if (void 0 === (s = _n)) return this; if (s === i || s.listener === i) 0 == --this._eventsCount ? this._events = Object.create(null) : (delete _n, _.removeListener && this.emit('removeListener', n, s.listener || i)); else if ('function' != typeof s) {
for (w = - 1, E = s.length - 1; E >= 0; E--) if (sE === i || sE.listener === i) { w = E;
break
}
if (w < 0) return this;
0 === w ? s.shift() : function spliceOne(n, i) {
for (; i + 1 < n.length; i++) ni = ni + 1; n.pop()
}(s, w),
1 === s.length && (_n = s0), void 0 !== _.removeListener && this.emit('removeListener', n, P || i)
}
return this
},
EventEmitter.prototype.off = EventEmitter.prototype.removeListener,
EventEmitter.prototype.removeAllListeners = function removeAllListeners(n) {
var i,
s,
_;
if (void 0 === (s = this._events)) return this;
if (void 0 === s.removeListener) return 0 === arguments.length ? (this._events = Object.create(null), this._eventsCount = 0) : void 0 !== sn && (0 == --this._eventsCount ? this._events = Object.create(null) : delete sn), this;
if (0 === arguments.length) {
var w,
E = Object.keys(s);
for (_ = 0; _ < E.length; ++_) 'removeListener' !== (w = E_) && this.removeAllListeners(w); return this.removeAllListeners('removeListener'),
this._events = Object.create(null),
this._eventsCount = 0,
this
}
if ('function' == typeof (i = sn)) this.removeListener(n, i); else if (void 0 !== i) for (_ = i.length - 1; _ >= 0; _--) this.removeListener(n, i_); return this
},
EventEmitter.prototype.listeners = function listeners(n) {
return _listeners(this, n, !0)
},
EventEmitter.prototype.rawListeners = function rawListeners(n) {
return _listeners(this, n, !1)
},
EventEmitter.listenerCount = function (n, i) {
return 'function' == typeof n.listenerCount ? n.listenerCount(i) : listenerCount.call(n, i)
},
EventEmitter.prototype.listenerCount = listenerCount,
EventEmitter.prototype.eventNames = function eventNames() {
return this._eventsCount > 0 ? _(this._events) : [
]
}
},
追加されたAPI一覧
scrapbox.addListener()
別名:scrapbox.on()
scrapbox.removeListener()
別名:scrapbox.off()
scrapbox.removeAllListeners()
scrapbox.once()
scrapbox.prependListener()
scrapbox.prependOnceListener()
scrapbox.rawListeners()
scrapbox.listeners()
scrapbox.listenerCount()
scrapbox.emit()
scrapbox.eventNames()
scrapbox.getMexListeners()
scrapbox.setMexListeners()
おそらく内部変数
scrapbox._events
scrapbox._eventsCount
scrapbox._maxListeners